package com.example.module_motorcircle.post.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.lib_net.model.api.PostItem
import com.example.module_motorcircle.R
import com.example.module_motorcircle.databinding.LayoutPostItemBinding


/**
 * @author yaotianxue
 * @description
 * @date 2025/2/11
 */
class PostAdapter(
    private val onItemClick:((PostItem,Int)->Unit)? = null,
    private val onLikeClick:((PostItem,Int)->Unit)? = null,
    private val onCollectionClick:((PostItem,Int)->Unit)? = null,
    private val onFollowClick:((PostItem,Int)->Unit)? = null,
    private val onMoreClick:((PostItem,Int)->Unit)? = null,
    private val onCommentClick:((PostItem,Int)->Unit)? = null,

): RecyclerView.Adapter<PostViewHolder>() {
    private val mList = mutableListOf<PostItem>()
    fun setData(data:List<PostItem>){
        mList.clear()
        mList.addAll(data)
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
        val binding = LayoutPostItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return PostViewHolder(
            binding,
            onItemClick,
            onLikeClick,
            onCollectionClick,
            onFollowClick,
            onMoreClick,
            onCommentClick
            )
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
       holder.binding.holder = holder //绑定点击事件
       holder.binding.post = mList[position] //绑定数据 简单ui使用databinding
       holder.initView(mList[position],position) //绑定数据 复杂ui手动实现 内容和九宫格
    }

    override fun onBindViewHolder(
        holder: PostViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        if(payloads.isNullOrEmpty()){
            onBindViewHolder(holder,position)
        }else if(payloads[0] == "updateLike"){
            //只更新点赞图标
            if(mList[position].ifLike == 1){
                holder.binding.ivLike.setImageResource(R.mipmap.icon_like_checked)
            }else{
                holder.binding.ivLike.setImageResource(R.mipmap.icon_like)
            }
            holder.binding.tvLike.text = mList[position].formatLikeNumber()
        }else if(payloads[0] == "updateCollect"){
            //只更新点赞图标
            if(mList[position].ifCollect == 1){
                holder.binding.ivCollection.setImageResource(R.mipmap.icon_collection_checked)
            }else{
                holder.binding.ivCollection.setImageResource(R.mipmap.icon_collection)
            }
            holder.binding.tvCollection.text = mList[position].formatCollectionNumber()
        }else if (payloads[0] == "updateFocus"){
            holder.binding.ivAdd.visibility = if (mList[position].showAddIcon()) View.VISIBLE else View.GONE
            holder.binding.ivMutual.visibility = if (mList[position].showMutualIcon()) View.VISIBLE else View.GONE
            holder.binding.tvFollowNameRed.visibility = if (mList[position].showAddIcon()) View.VISIBLE else View.GONE
            holder.binding.tvFollowNameGray.visibility = if (mList[position].showAddIcon()) View.GONE else View.VISIBLE
            holder.binding.tvFollowNameGray.text = mList[position].followStateName()
            holder.binding.tvFollowNameRed.text = mList[position].followStateName()
            //边框
            holder.binding.lFollowState.isSelected = if (mList[position].showAddIcon()) false else true
        }
    }

    fun getPostItem(position:Int):PostItem{
        return mList[position]
    }
}